package com.nimitz.module.employee.service.impl;

import com.nimitz.common.annotation.DataScope;
import com.nimitz.common.utils.DateUtils;
import com.nimitz.common.utils.SecurityUtils;
import com.nimitz.module.bi.service.IBiEmployeeService;
import com.nimitz.module.employee.domain.EmpEmployee;
import com.nimitz.module.employee.domain.EmpInduction;
import com.nimitz.module.employee.domain.EmpOfficial;
import com.nimitz.module.employee.mapper.EmpEmployeeMapper;
import com.nimitz.module.employee.mapper.EmpInductionMapper;
import com.nimitz.module.employee.mapper.EmpOfficialMapper;
import com.nimitz.module.employee.service.IEmpInductionService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 入职 业务层实现
 */
@Service
public class EmpInductionServiceImpl implements IEmpInductionService {

    @Resource
    private EmpInductionMapper inductionMapper;
    @Resource
    private EmpEmployeeMapper employeeMapper;
    @Resource
    private EmpOfficialMapper officialMapper;
    @Resource
    private IBiEmployeeService employeeService;

    /**
     * 修改入职
     *
     * @param induction 入职
     * @return 结果
     */
    @Override
    @Transactional
    public int updateInduction(EmpInduction induction) {
        // 检查人员对象是否为空
        if (induction.getInductionEmployee() != null) {
            // 修改人员对象
            updateEmployee(induction);
        }
        induction.setInductionOperateName(SecurityUtils.getLoginUser().getUsername());
        induction.setInductionOperateDate(DateUtils.getDate());
        return inductionMapper.updateInduction(induction);
    }

    /**
     * 根据入职id查询入职
     *
     * @param inductionId 入职id
     * @return 入职
     */
    @Override
    public EmpInduction selectInductionById(Integer inductionId) {
        return inductionMapper.selectInductionById(inductionId);
    }

    /**
     * 根据条件查询入职列表（含分页）
     *
     * @param induction 入职
     * @return 入职集合
     */
    @Override
    @DataScope(deptAlias = "tn1")
    public List<EmpInduction> selectInductionList(EmpInduction induction) {
        return inductionMapper.selectInductionList(induction);
    }

    /**
     * 批量提交
     *
     * @param updateBy     更新者
     * @param inductionIds 入职ID集合
     * @return 结果
     */
    @Override
    public int submitInduction(String updateBy, Integer[] inductionIds) {
        return inductionMapper.submitInduction(updateBy, inductionIds);
    }

    /**
     * 批量驳回
     *
     * @param updateBy     更新者
     * @param inductionIds 入职ID集合
     * @return 结果
     */
    @Override
    public int rejectInduction(String updateBy, Integer[] inductionIds) {
        return inductionMapper.rejectInduction(updateBy, inductionIds);
    }

    /**
     * 批量审核
     *
     * @param updateBy     更新者
     * @param inductionIds 入职ID集合
     * @param employeeIds  人员dID集合
     * @return 结果
     */
    @Override
    @Transactional
    public int auditInduction(String updateBy, Integer[] inductionIds, Integer[] employeeIds) {
        int returnVal = inductionMapper.auditInduction(updateBy, inductionIds);
        if (returnVal > 0) {
            // 批量修改employee状态
            employeeMapper.auditEmployee(updateBy, employeeIds);
            // 根据employeeIds获取集合
            List<EmpEmployee> employeeList = employeeMapper.selectEmployeeByIds(employeeIds);
            // 生成转正记录集合
            List<EmpOfficial> officialList = new ArrayList<>();
            for (EmpEmployee employee : employeeList) {
                EmpOfficial official = new EmpOfficial();
                official.setEmployeeId(employee.getEmployeeId());
                // 入职日期+6个月
                if (employee.getEmployeeInductionDate() != null) {
                    Date inductionDate = DateUtils.parseDate(employee.getEmployeeInductionDate());
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(inductionDate);
                    calendar.add(Calendar.MONTH, 6);
                    official.setOfficialDate(DateUtils.dateTime(calendar.getTime()));
                }
                official.setCreateBy(SecurityUtils.getLoginUser().getUsername());
                officialList.add(official);
            }
            // 批量生成转正记录
            officialMapper.insertOfficial(officialList);
            // 生成仪表盘
            employeeService.generateEmployee(DateUtils.getDate());
        }
        return returnVal;
    }

    /**
     * 修改人员对象
     *
     * @param induction
     */
    private void updateEmployee(EmpInduction induction) {
        EmpEmployee employee = new EmpEmployee();
        employee.setEmployeeId(induction.getEmployeeId());
        employee.setEmployeeCode(induction.getInductionEmployee().getEmployeeCode());
        employee.setEmployeeLevel(induction.getInductionEmployee().getEmployeeLevel());
        employee.setEmployeeInductionDate(induction.getInductionEmployee().getEmployeeInductionDate());
        employee.setEmployeeRecruitType(induction.getInductionEmployee().getEmployeeRecruitType());
        employee.setEmployeeRecruitBatch(induction.getInductionEmployee().getEmployeeRecruitBatch());
        employee.setEmployeeRecruitDate(induction.getInductionEmployee().getEmployeeRecruitDate());
        employee.setEmployeeArchives(induction.getInductionEmployee().getEmployeeArchives());
        employee.setUpdateBy(SecurityUtils.getLoginUser().getUsername());
        employeeMapper.updateEmployee(employee);
    }
}
